﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Diagnostics;
using System.Diagnostics;
using System.Reflection.Metadata;

namespace 填数字游戏
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            int[,] ints = new int[9, 9];


            for (int i = 0; i < 9; i++)
            {
                for (int k = 0; k < 9; k++)
                {
                    ComboBox comboBox = new ComboBox();
                                    
                    comboBox.ItemsSource =new int[]{1,2,3,4,5,6,7,8,9,0};
                    comboBox.VerticalContentAlignment = VerticalAlignment.Center;
                    comboBox.HorizontalContentAlignment= HorizontalAlignment.Center;
                    comboBox.FontSize = 20;
                    Grid.SetRow(comboBox, i);
                    Grid.SetColumn(comboBox, k);
                    comboBox.Background = Brushes.Red;
                    this.myGrid.Children.Add(comboBox);

                    

                }

            }
            Debug.Write("aaa");

        }

        static int[,] pu = new int[9, 9];
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("点击了按钮");
            ComboBox? cb;
            
            int dgnum = 0;
           
            for (int i = 0; i < 9; i++)
            {
                for (int k = 0; k < 9; k++)
                {
                    cb = myGrid.Children[dgnum+1] as ComboBox;
                    dgnum++;
                    if (cb is ComboBox)
                    {
                        Debug.WriteLine(cb.Text.ToString());
                        pu[i, k] = string.IsNullOrEmpty(cb.Text) ? 0 : int.Parse(cb.Text);
                        
                    }
                }
                
            }





            listShow();

            GetAnswer(0);


            foreach (var item in pu)
            {

                Debug.Write(item.ToString());
                Debug.Write(",");
            }


            //显示结果
            int temp = 0;
         
            for (int i = 0; i < 9; i++)
            {
                for (int k = 0; k < 9; k++)
                {

                    cb = myGrid.Children[temp + 1] as ComboBox;
                    temp++;
                    if (cb is ComboBox)
                    {
                        cb.Text = pu[i, k].ToString();

                    }
                }

            }





        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ComboBox? tComboBox;
            foreach (var item in myGrid.Children)
            {
                tComboBox=item as ComboBox;
                if (tComboBox is ComboBox)
                {
                    tComboBox.Text = "";
                }


            }

        }


        /// <summary>

        /// 验证函数

        /// </summary>

        /// <param name="i"></param>

        /// <param name="j"></param>

        /// <returns></returns>

        static bool IsValid(int i, int j)

        {

            int n = pu[i, j];

            int[] query = new int[9] { 0, 0, 0, 3, 3, 3, 6, 6, 6 };



            int t, u;

            //每一行每一列是否重复

            for (t = 0; t < 9; t++)

            {

                if ((t != i && pu[t, j] == n) || (t != j && pu[i, t] == n))

                    return false;

            }

            //每个九宫格是否重复

            for (t = query[i]; t < query[i] + 3; t++)

            {

                for (u = query[j]; u < query[j] + 3; u++)

                {

                    if ((t != i || u != j) && pu[t, u] == n)

                        return false;

                }

            }

            return true;



        }

        /// <summary>

        /// 显示函数

        /// </summary>

        static void listShow()

        {
            
            for (var i = 0; i < 9; i++)

            {

                for (var j = 0; j < 9; j++)

                {

                    Debug.Write(pu[i, j] + " ");                                            



                }

                Debug.WriteLine("");

            }



            Debug.WriteLine("----------------------------------------------");







        }



        /// <summary>

        /// 使用回溯算法求解

        /// </summary>

        /// <param name="n"></param>

        static void GetAnswer(int n)

        {

            if (n == 81)

            {//是否已经是最后一个格子

                listShow();

                return;

            }



            int i = n / 9, j = n % 9;



            if (pu[i, j] != 0)

            {//如果当前格子不需要填数字，就跳到下一个格子

                GetAnswer(n + 1);

                return;

            }



            for (int k = 0; k < 9; k++)

            {

                pu[i, j]++;//当前格子进行尝试所有解

                if (IsValid(i, j))

                    GetAnswer(n + 1);//验证通过，就继续下一个

            }



            pu[i, j] = 0;  //如果上面的单元无解，还原，就回溯

            return;

        }

    }
}